const sql = require("../../mysql");

module.exports = async ctx => {
  try {
    console.log(ctx.request.userId);

    const { name, parentId } = ctx.request.body;
    if (name.trim() === "") {
      ctx.body = {
        code: -1,
        msg: "请输入文件名"
      };
      return;
    }

    let folder = await sql.Vfile.create({
      name: name.trim(),
      isFolder: true,
      userId: ctx.request.userId,
      parentId
    });

    ctx.body = {
      code: 0,
      data: folder,
      msg: "新增成功！"
    };
  } catch (error) {
    console.log(error);
    ctx.response.status = 200;

    ctx.body = {
      code: -1,
      msg: error.errors ? error : error.errors[0].message
    };
  }
};
